package com.zwy.Shopping.servlet_;

import com.zwy.Shopping.dao.OrderDao;
import com.zwy.Shopping.dao.OrderItemDao;
import com.zwy.Shopping.dao.ProductDao;
import com.zwy.Shopping.dao.impl.OrderDaoImpl;
import com.zwy.Shopping.dao.impl.OrderItemDaoImpl;
import com.zwy.Shopping.dao.impl.ProductDaoImpl;
import com.zwy.Shopping.model.Product;
import com.zwy.Shopping.model.User;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet("/now_shopping_order")
public class NowShoppingOrderServlet extends HttpServlet {
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String buyNumber = request.getParameter("buyNumber");
        String totalMoney = request.getParameter("totalMoney");
        Product product = (Product)request.getSession().getAttribute("now_shopping_product");
        if(product == null){
            request.setAttribute("message","获取产品对象失败");
            request.setAttribute("page","index.jsp");
            request.getRequestDispatcher("alert.jsp").forward(request,response);
        }
        System.out.println(product);
          User user = (User)request.getSession().getAttribute("user");
        System.out.println(user);
          if(user == null){
              request.setAttribute("message","您没有登录,请登陆后购买");
              request.setAttribute("page","login.jsp");
              request.getRequestDispatcher("alert.jsp").forward(request,response);
          }else {
              int userId = user.getId();
              OrderDao orderDao = new OrderDaoImpl();
              System.out.println(userId);
              int orderId = orderDao.generateOrder(userId, new BigDecimal(totalMoney));
              System.out.println(orderId);

              //到这都是对的

              try {
                  System.out.println(product.getId());
                  System.out.println(product.getPrice());
                  System.out.println(Integer.parseInt(buyNumber));
                  OrderItemDao orderItemDao = new OrderItemDaoImpl();
                  boolean isBuySuccessful = orderItemDao.generateOrderItem(orderId, product.getId(), product.getPrice(), Integer.parseInt(buyNumber));
                  if (isBuySuccessful) {
                      ProductDao productDao = new ProductDaoImpl();
                     if(productDao.reduceProductStock(product.getId(),Integer.parseInt(buyNumber)))
                         System.out.println("库存减少成功");
                      request.setAttribute("message", "购买成功,返回主界面");
                      request.setAttribute("page", "index.jsp");
                      request.getRequestDispatcher("alert.jsp").forward(request, response);
                  } else {
                      request.setAttribute("message", "购买失败请重试");
                      request.setAttribute("page", "now_shopping.jsp");
                      request.getRequestDispatcher("alert.jsp").forward(request, response);
                  }


              }catch (Exception e){
                  e.printStackTrace();
              }
          }
    }
}
